function [ts1,ts2] = Z_time_to_name(td)
% 8/17/2011 Parker MacCready
%
% this takes a datenum-format time (a number) and returns a string that is
% used to identify an mm5 file
%
% assumes that td is "on the hour" (min=0, sec=0)
%
% this is designed to reproduce the strange mm5 file numbering system, e.g.:
% June 15, 2006, 17:00 hours is named with the string:
%   2006061500.f17
%   yyyymmddFF.fhh
% "FF" is the "forecast hour" meaning the time on that day when this
% forecast began, and hh is referenced to this time.
% So, when FF = 00 then it is indeed hour 17:00 (hh goes 12-24 in all cases)
% BUT when FF = 12 then you add 12 to the hours, meaning that it would be
% 05:00 hours the morning of the next day, so
% June 16, 2006, 05:00 hours is named with the string:
%   2006061512.f17 (NOTE we still have dd = 15, but ^^ = 12)
%
% the two output strings of this function just correspond to the two parts
% that might be handy to have later, so:
%   ts1 = 2006061512 (used often as a folder name), and
%   ts2 = .f17
%
% so the full path to an mm5 output file might be specified like:
%   indir = '../data/pp2006/jun/';
%   fn = [indir,'ppocean_mmout_',grd,'.',ts1,ts2,'.0000'];
% where "grd" is d2 or d3

yy = datestr(td,'yyyy');
y = str2num(yy);
mm = datestr(td,'mm');
m = str2num(mm);
dd = datestr(td,'dd');
d = str2num(dd);
hh = datestr(td,'HH:MM'); hh = hh(1:2);
h = str2num(hh);

% also find values for the day before (required when crossing month or year
% boundaries)
td1 = td-1;
yy1 = datestr(td1,'yyyy');
y1 = str2num(yy1);
mm1 = datestr(td1,'mm');
m1 = str2num(mm1);
dd1 = datestr(td1,'dd');
d1 = str2num(dd1);

% and decide which forecast hour to use, '00' or '12'
if h>=12 & h<=24
    % note that this will always miss the .f24 file in a "00"
    % sequence (the "thirteenth hour" in the folder), but that hour is
    % repeated as .f12 in the subsequent "12" sequence
    forecast_hour = '00';
else
    forecast_hour = '12';
    yy = ['0000',yy1]; yy = yy(end-3:end);
    mm = ['00',mm1]; mm = mm(end-1:end);
    dd = ['00',dd1]; dd = dd(end-1:end);
    hh = ['00',num2str(h+12)]; hh = hh(end-1:end);
end

ts1 = [yy,mm,dd,forecast_hour];
ts2 = ['.f',hh];
    
    